package com.victory.calculator.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aries.ui.view.title.TitleBarView;
import com.victory.calculator.R;
import com.victory.calculator.api.Api;
import com.victory.calculator.entity.ApplyEntity;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.mode.CacheMode;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.entity.BaseTResp;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.OnNoDoubleClickListener;
import tech.com.commoncore.utils.ToastUtil;

/**
 * 申请份额计算
 */
public class ApplyActivity extends BaseTitleActivity {


    private EditText etMoney;
    private EditText etNetWorth;
    private EditText etRate;
    private Button btnStart;
    private Button btnAgain;
    private TextView tvServiceCharge;
    private TextView tvApplyForShare;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.calculator_apply_title));
    }

    @Override
    public int getContentLayout() {
        return R.layout.calculator_activity_apply;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        etMoney = (EditText) findViewById(R.id.et_money);
        etNetWorth = (EditText) findViewById(R.id.et_net_worth);
        etRate = (EditText) findViewById(R.id.et_rate);
        btnStart = (Button) findViewById(R.id.btn_start);
        btnAgain = (Button) findViewById(R.id.btn_again);
        tvServiceCharge = (TextView) findViewById(R.id.tv_service_charge);
        tvApplyForShare = (TextView) findViewById(R.id.tv_apply_for_share);


        //开始计算
        btnStart.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (DataUtils.isEmpty(etMoney.getText())) {
                    ToastUtil.show(getResources().getString(R.string.calculator_apply_et_money));
                    return;
                }
                if (DataUtils.isEmpty(etNetWorth.getText())) {
                    ToastUtil.show(getResources().getString(R.string.calculator_apply_et_net_worth));
                    return;
                }
                if (DataUtils.isEmpty(etRate.getText())) {
                    ToastUtil.show(getResources().getString(R.string.calculator_apply_et_rate));
                    return;
                }
                String price = etMoney.getText().toString().trim();
                String fund_value = etNetWorth.getText().toString().trim();
                String rate = etRate.getText().toString().trim();
                if (Double.parseDouble(rate)>0){
                    if (Double.parseDouble(rate)<=1.5){
                        showLoading();
                        start(price, fund_value, rate);
                    }else {
                        ToastUtil.show(getResources().getString(R.string.calculator_rate));
                        return;
                    }
                }else {
                    ToastUtil.show(getResources().getString(R.string.calculator_rate));
                    return;
                }
            }
        });
        //重新计算
        btnAgain.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        etMoney.setText("");
                        etNetWorth.setText("");
                        etRate.setText("");
                        tvServiceCharge.setText("");
                        tvApplyForShare.setText("");
                    }
                });
            }
        });

    }

    private void start(String price, String fund_value, String rate) {
        ViseHttp.GET(Api.APPLY_URL)
                .baseUrl(ApiConstant.BASE_URL)
                .addParam("price", price)
                .addParam("fund_value", fund_value)
                .addParam("rate", rate)
                .setHttpCache(true)
                .cacheMode(CacheMode.FIRST_REMOTE)
                .request(new ACallback<BaseTResp<ApplyEntity>>() {
                    @Override
                    public void onSuccess(final tech.com.commoncore.entity.BaseTResp<ApplyEntity> data) {
                        try {
                            if (data.data != null) {
                                hideLoading();
                                new Handler().post(new Runnable() {
                                    @Override
                                    public void run() {
                                        tvServiceCharge.setText(data.data.getSubscription_fee());
                                        tvApplyForShare.setText(data.data.getSubscription_share() + "");
                                    }
                                });
                            } else {
                                hideLoading();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                    }
                });
    }
}
